package igz.fleaxy.locale
{
	import igz.fleaxy.net.Comm;
	import igz.fleaxy.util.DataTableUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 *
	 * Clase gestora del sistema multi-idioma
	 */
	public class LocaleManager {
		static private var _Locales : Object;

		static private var _Iso639 : String;
		static public function get $Iso639() : String {
			return _Iso639;
		}
		
		protected var _BlockName:String = "Lang";
		protected var _Iso639ColumnName:String = "FkIso639@LangValue";
		protected var _ScopeColumnName:String = "FkCdScope@LangValue";
		protected var _LabelColumnName:String = "FkCdLabel@LangValue";
		protected var _ValueColumnName:String = "Value@LangValue";

		protected var _OnComplete:Function;

		public function LocaleManager() {
			_Locales = new Object();
		}

		/**
		 * Carga el fichero XML de multi-idioma
		 *
		 * @param	p_iso639Uri url con el fichero multi-idioma
		 * @param	f_onComplete función que se ejecutará al terminar la caarga
		 */
		public function $Load(p_iso639Uri:String, f_onComplete : Function ) :void {
			Comm.$Get(p_iso639Uri, { "onComplete" : f_onComplete } );
		}

		public function $SetIso639(p_iso639:String ) :void {
			_Iso639 = p_iso639;
		}

		/**
		 * Carga los datos desde un XML
		 * @param	p_xml XML con los datos de idioma
		 */
		public function $LoadFromXML(p_xml:XML) : void {
			if ( !_Locales.hasOwnProperty(p_xml.name()) ) {
				_Locales[p_xml.name()] = new Object();
			}
			var scopeC:uint;
			var scopes:XMLList = p_xml.elements();
			var labelC:uint;
			var labels:XMLList;
			for each ( var scope:XML in scopes ) {
				if ( !_Locales[p_xml.name()].hasOwnProperty(scope.name()) ) {
					_Locales[p_xml.name()][scope.name()] = new Object();
				}
				for each ( var label:XML in scope.elements() ) {
					if ( !_Locales[p_xml.name()][scope.name()].hasOwnProperty(label.name()) ) {
						_Locales[p_xml.name()][scope.name()][label.name()] = new Object();
					}
					_Locales[p_xml.name()][scope.name()][label.name()] = label
				}
			}
		}

		public function $LoadFromJSON(p_json:Object) : void {
			var rootTag : String = "";
			for ( rootTag in p_json ) { break; }
			
			var dtLang:Object = p_json[rootTag][_BlockName];
			var iso639:String,scope:String,label:String;
			var f:uint, F:uint = DataTableUtil.$Length(dtLang);
			for ( f = 0; f < F; f++ ) {
				iso639 = DataTableUtil.$ValueOf(dtLang, _Iso639ColumnName, f);
				scope  = DataTableUtil.$ValueOf(dtLang, _ScopeColumnName, f);
				label  = DataTableUtil.$ValueOf(dtLang, _LabelColumnName, f);
				if ( !_Locales.hasOwnProperty(iso639 ) ) {
					_Locales[iso639] = new Object();
				}
				if ( !_Locales[ iso639 ].hasOwnProperty(scope) ) {
					_Locales[ iso639 ][scope] = new Object();
				}
				if ( !_Locales[iso639][scope].hasOwnProperty(label) ) {
					_Locales[iso639][scope][label] = new Object();
				}
				_Locales[iso639][scope][label] = DataTableUtil.$ValueOf(dtLang, _ValueColumnName, f);
			}
		}

		/**
		 * Devuelve el texto de un determinado locale
		 * @param	p_scope Scope del texto
		 * @param	p_label Etiqueta del texto
		 * @param	p_params Parámetros del texto
		 * @return Texto en el idimoma
		 *
		 * @example $GetText("Global","wellcome")
		 */
		static public function $GetText(p_scope:String, p_label:String, p_params:Object = null) : String {
			return $GetTextByIso639( _Iso639,  p_scope, p_label, p_params);
		}

		static public function $GetScope(p_scope:String) : Object {
			return $GetScopeByIso639( _Iso639,  p_scope);
		}

		static public function $GetScopeByIso639(p_scope:String, p_iso639:String) : Object {
			var scopes:Object = _Locales[p_iso639] as Object;
			return scopes[p_scope] as Object;
		}

		static public function $GetTextByIso639(p_iso639:String, p_scope:String, p_label:String, p_params:Object = null) : String {
			var value:String = null;

			if ( _Locales.hasOwnProperty(p_iso639 ) ) {
				if ( _Locales[ p_iso639 ].hasOwnProperty(p_scope) ) {
					if ( _Locales[p_iso639][p_scope].hasOwnProperty(p_label) ) {
						value = _Locales[p_iso639][p_scope][p_label];
						if ( value != null ) {
							for ( var param:String in p_params ) {
								value = value.replace( param, p_params[param] );
							}
							value = value.replace(/\n/gi, ""); // Salto de linea
							value = value.replace(/\r/gi, ""); // Retorno de carro
							value = value.replace(/\t/gi, ""); // Tabulación
						}
					}
					else {
						value = "NOTEXT[" + p_iso639 + "]["+p_scope+"]["+p_label+"]";
					}
				}
				else {
					value = "NOTEXT[" + p_iso639 + "]["+p_scope+"]";
				}
			}
			else {
				value = "NOTEXT[" + p_iso639 + "]";
			}

			return value;
		}

	}
	
}